<?php

/**
 * @file classes/manager/form/HDBANHANGForm.inc.php
 *
 * Copyright (c) 2003-2012 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class HDBANHANGForm
 * @ingroup manager_form
 *
 * @brief Form for creating and modifying journal HDBANHANGs.
 */

// $Id$


import('lib.pkp.classes.form.Form');

class HDBANHANGForm extends Form {

	/** @var $HDBANHANGId int The ID of the HDBANHANG being edited */
	var $HDBANHANGId;

	/** @var $includeHDBANHANGEditor object Additional HDBANHANG editor to
	 *       include in assigned list for this HDBANHANG
	 */
	var $includeHDBANHANGEditor;

	/** @var $omitHDBANHANGEditor object Assigned HDBANHANG editor to omit from
	 *       assigned list for this HDBANHANG
	 */
	var $omitHDBANHANGEditor;

	/** @var $HDBANHANGEditors array List of user objects representing the
	 *       available HDBANHANG editors for this journal.
	 */
	var $HDBANHANGEditors;

	/**
	 * Constructor.
	 * @param $journalId int omit for a new journal
	 */
	function HDBANHANGForm($HDBANHANGId = null) {

		parent::Form('acctCN/CTBANHANGForm.tpl');

		$journal =& Request::getJournal();
		$this->HDBANHANGId = $HDBANHANGId;

		// Validation checks for this form
		$this->addCheck(new FormValidatorLocale($this, 'matk', 'required', 'Phai co Ma TK!'));
		$this->addCheck(new FormValidatorLocale($this, 'tentk', 'required', 'Phai co Ten TK'));
		$this->addCheck(new FormValidatorPost($this));
	

		// Get a list of HDBANHANG editors for this journal.
		$roleDao =& DAORegistry::getDAO('RoleDAO');
		$this->HDBANHANGEditors =& $roleDao->getUsersByRoleId(ROLE_ID_TMTG_EDITOR, $journal->getId());
		$this->HDBANHANGEditors =& $this->HDBANHANGEditors->toArray();		
	}

	/**
	 * When displaying the form, include the specified HDBANHANG editor
	 * in the assigned list for this HDBANHANG.
	 * @param $HDBANHANGEditorId int
	 */
	function includeHDBANHANGEditor($HDBANHANGEditorId) {
		foreach ($this->HDBANHANGEditors as $key => $junk) {
			if ($this->HDBANHANGEditors[$key]->getId() == $HDBANHANGEditorId) {
				$this->includeHDBANHANGEditor =& $this->HDBANHANGEditors[$key];
			}
		}
	}

	/**
	 * When displaying the form, omit the specified HDBANHANG editor from
	 * the assigned list for this HDBANHANG.
	 */
	function omitHDBANHANGEditor($HDBANHANGEditorId) {
		foreach ($this->HDBANHANGEditors as $key => $junk) {
			if ($this->HDBANHANGEditors[$key]->getId() == $HDBANHANGEditorId) {
				$this->omitHDBANHANGEditor =& $this->HDBANHANGEditors[$key];
			}
		}
	}

	/**
	 * Get the names of fields for which localized data is allowed.
	 * @return array
	 */
	function getLocaleFieldNames() {
		$HDBANHANGDao =& DAORegistry::getDAO('HDBANHANGDAO');
		return $HDBANHANGDao->getLocaleFieldNames();
	}

	/**
	 * Display the form.
	 */
	function display() {
		$journal =& Request::getJournal();
		
		parent::display();
	}

	/**
	 * Initialize form data from current settings.
	 */
	function initData() {
		$journal =& Request::getJournal();
		if (isset($this->HDBANHANGId)) {
			$HDBANHANGDao =& DAORegistry::getDAO('HDBANHANGDAO');
			$HDBANHANG =& $HDBANHANGDao->getHDBANHANG($this->HDBANHANGId, false, false);
			if ($HDBANHANG == null) {
				unset($this->HDBANHANGId);
			} else {
				$this->_data = array(
					'matk' => $HDBANHANG->getmatk(), // Localized
					'tentk' => $HDBANHANG->gettentk(), // Localized
				);
			}
		}
	}

	/**
	 * Assign form data to user-submitted data.
	 */
	function readInputData() {
		$this->readUserVars(array('matk', 'tentk'));
	}

	/**
	 * Save HDBANHANG.
	 */
	function execute() {

		$HDBANHANGDao =& DAORegistry::getDAO('HDBANHANGDAO');

		if (isset($this->HDBANHANGId)) {
			$HDBANHANG =& $HDBANHANGDao->getHDBANHANG($this->HDBANHANGId, false, false);
		}

		if (!isset($HDBANHANG)) {
			$HDBANHANG = new HDBANHANG();
		}

		$HDBANHANG->setmatk($this->getData('matk'), null); // Localized
		$HDBANHANG->settentk($this->getData('tentk'), null); // Localized

		
		if (isset($this->HDBANHANGId)) {
			$HDBANHANGDao->updateHDBANHANG($HDBANHANG);
			$HDBANHANGId = $HDBANHANG->getId();

		} else {

			$HDBANHANGId = $HDBANHANGDao->insertHDBANHANG($HDBANHANG);
			
		}

	}
}

?>
